package com.bk.servlet;

import com.bk.service.BookService;
import com.bk.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTML;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 27435
 * @Date 2024/11/2519:19
 * @Version 1.0
 */

@WebServlet(urlPatterns = {"/deleteBook"})
public class DeleteBookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求编码
        req.setCharacterEncoding("UTF-8");
        // 设置响应编码
        resp.setCharacterEncoding("UTF-8");
        // 设置响应类型
        resp.setContentType("text/html;charset=UTF-8");
        // 获取要删除数据的id
        String id = req.getParameter("id");
        // 响应流
        PrintWriter out = resp.getWriter();
        // 调用业务层查询所有图书的方法
        BookService bookService = new BookServiceImpl();
        // 根据id删除图书
        int i = bookService.deleteBookById(Integer.valueOf(id));
        // 判断是否删除成功
        if (i > 0) {
            out.print("删除成功");
        }else {
            out.print("删除失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
